package com.itttq.gen.common.conf;

import com.itttq.gen.common.constant.Constants;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties(prefix = "gen")
@Data
public class GenProperties {
    private static final String PROJECT_DIR = "project/";
    private static final String SQL_DIR = "sql/";
    /**
     * 只需要项目配置的模板文件地址
     */
    private String tmplProjectFlord;
    /**
     * 需要表字段配置的模板文件地址
     */
    private String tmplGeneratorFlord;
    /**
     * 生存放目录的父目录
     */
    private String parentDir;
    /**
     * 项目文件下载URL
     */
    private String downloadUrl;
    /**
     * 生成项目的目录
     */
    private String projectDir;
    /**
     * 上传SQL文件存放地址
     */
    private String sqlFlord;

    public String getSqlFlord() {
        return sqlFlord;
    }

    public void setParentDir(String parentDir) {
        this.parentDir = parentDir;
        String subDir = parentDir.endsWith(Constants.SLASH) || parentDir.endsWith(Constants.BACKSLASH) ? "" : Constants.SLASH;
        this.projectDir = parentDir + subDir + PROJECT_DIR;
        this.sqlFlord = parentDir + subDir + SQL_DIR;
    }

    public String getSqlDownloadUrl(String fileName) {
        return getDownloadUrlPath() + SQL_DIR + fileName;
    }

    public String getProjectDownloadUrl(String fileName) {
        return getDownloadUrlPath() + PROJECT_DIR + fileName;
    }

    private String getDownloadUrlPath() {
        return downloadUrl + (downloadUrl.endsWith(Constants.SLASH) || downloadUrl.endsWith(Constants.BACKSLASH) ? "" : Constants.SLASH);
    }
}
